<template>
  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item label="信息描述">
        <el-tag>
          <a type="text" @click="notice">excel模版说明</a>
        </el-tag>
        <el-tag>
          <i class="el-icon-download" />
          <a :href="defaultExcelTemplate">点击下载模版</a>
        </el-tag>
      </el-form-item>

      <!-- 上传 -->
      <el-form-item label="选择Excel">
        <el-upload
          ref="upload"
          :auto-upload="false"
          :on-exceed="handleSize"
          :on-success="handleSuccess"
          :on-error="handleError"
          :limit="1"
          :file-list="fileList"
          :on-change="handleChange"
          action="http://localhost:8110/admin/edu/subject/import"
          name="file"
          accept="application/vnd.ms-excel"
        >
          <el-button slot="trigger" size="small" type="primary"
            >选取文件</el-button
          >
          <el-button
            :disabled="importBtnDisabled"
            style="margin-left: 10px"
            size="small"
            type="success"
            @click="submitUpload()"
            >上传到服务器</el-button
          >
          <div slot="tip" class="el-upload__tip">只能上传课程分类Excel文档</div>
        </el-upload>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      //记录文件列表数
      count: 0,
      fileList:[],
      importBtnDisabled: false, // 导入按钮是否禁用
      defaultExcelTemplate:
       process.env.OSS_PATH + '/excel/课程分类列表模板.xls',
    // "https://shusu-online-file.oss-cn-beijing.aliyuncs.com"+ '/excel/课程分类列表模板.xls'

    }
  },
  methods: {
    notice() {
      this.$alert(
        "导入课程分类的提示内容,请一定参考格式编写提交,否则不能处理!!",
        "Excel模板说明",
        {
          confirmButtonText: "确定",
        }
      );
    },
    //当上传文件数超出限制时的回调函数
    handleSize(files, fileList) {
      this.count = files.length;
      this.$message.warning("最大只能上传1个文件! 当前文件数 :" + files.length);
    },
    //上传文件
    submitUpload() {
      if (this.count === 0) {
        this.$message.warning("请选择文件后再上传! 当前文件数 :" + this.count);
        return;
      }
      this.$refs.upload.submit();
    },
    handleChange() {
      this.count = 1;
    },
    //上传成功之后清0
    handleSuccess() {
      this.count = 0;
      //清除上传成功之后的文件列表
      this.fileList = []
    },
    //删除文件之后的回调
    handleRemove(file, fileList) {
      this.count = 0;
    },
    //上传失败的回调
    handleError() {
      this.importBtnDisabled = false; // 启用按钮
      this.$message.error("上传失败! （http失败）");
      this.$refs.upload.clearFiles(); // 清空文件列表
    },
    handlePreview(file) {
      console.log(file);
    },

    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
  },
};
</script>
